package com.wei.czz.framework.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.store.StoreSubmitTypeEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.po.store.AssignStoreListPo;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.common.vo.store.assign.AssignFormVo;
import com.wei.czz.common.vo.store.assign.AssignParamVo;
import com.wei.czz.common.vo.store.assign.AssignVo;
import com.wei.czz.framework.store.dao.AssignStoreDao;
import com.wei.czz.framework.store.entity.AssignStoreEntity;
import com.wei.czz.framework.store.entity.StoreEntity;
import com.wei.czz.framework.store.service.AssignStoreService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2023-12-01 11:37:24
 * className: AssignStoreServiceImpl
 * version: 1.0
 * description:
 */
@Service("assignStoreService")
@AllArgsConstructor
public class AssignStoreServiceImpl extends ServiceImpl<AssignStoreDao, AssignStoreEntity>
        implements AssignStoreService {

    private static final Logger log = LoggerFactory.getLogger(AssignStoreServiceImpl.class);

    @Override
    public void saveAssignStore(Long storeId, Integer submitType) {
        LambdaQueryWrapper<AssignStoreEntity> assignStoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
        assignStoreLambdaQueryWrapper.eq(AssignStoreEntity::getStoreId, storeId)
                .eq(AssignStoreEntity::getSubmitType, submitType)
                .eq(AssignStoreEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 统计数量
        Long num = baseMapper.selectCount(assignStoreLambdaQueryWrapper);
        if (num > 0) {
            log.info("商店审核分单数据已存在。storeId={} submitType={}", storeId, StoreSubmitTypeEnum.getName(submitType));
            return;
        }

        // 获取正在操作的用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        AssignStoreEntity assignStore = new AssignStoreEntity();
        assignStore.setUserId(CommonEnum.MINUS_ONE.getLongValue())
                .setStoreId(storeId)
                .setSubmitType(submitType)
                .setAssignType(CommonEnum.MINUS_ONE.getValue())
                .setDeleteStatus(CommonEnum.ZERO.getValue())
                .setUpdateTime(date)
                .setUpdateUser(StringUtils.EMPTY)
                .setUpdateUserId(CommonEnum.ZERO.getLongValue())
                .setCreateTime(date)
                .setCreateUser(userPo.getUsername())
                .setCreateUserId(userPo.getUserId());

        int count = baseMapper.insert(assignStore);
        log.info("保存商店审核分单数据完成。count={}", count);
    }

    @Override
    public PageDto<AssignStoreListPo> getAssignStorePageList(AssignFormVo assignFormVo) {

        // mp分页对象
        Page<AssignStoreListPo> page = new Page<>(assignFormVo.getPage(), assignFormVo.getLimit());
        // 分页查询
        baseMapper.selectAssignStorePageList(page, assignFormVo);

        return new PageDto<>(page);
    }

    @Override
    public PageDto<AssignStoreListPo> getAssignableStorePageList(AssignFormVo assignFormVo) {

        // mp分页对象
        Page<AssignStoreListPo> page = new Page<>(assignFormVo.getPage(), assignFormVo.getLimit());
        // 分页查询
        baseMapper.selectAssignableStorePageList(page, assignFormVo);

        return new PageDto<>(page);
    }

    @Override
    public List<AssignStoreEntity> getassignStoreList(Collection<Long> userIdCollection) {
        if (userIdCollection.isEmpty()) {
            log.info("传入用户主键集合为空");
            return Collections.emptyList();
        }
        LambdaQueryWrapper<AssignStoreEntity> assignStoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
        assignStoreLambdaQueryWrapper.in(AssignStoreEntity::getUserId, userIdCollection)
                .eq(AssignStoreEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<AssignStoreEntity> assignStoreList = baseMapper.selectList(assignStoreLambdaQueryWrapper);
        if (assignStoreList.isEmpty()) {
            log.info("商店分单列表为空。userIdCollection={}", userIdCollection);
            return Collections.emptyList();
        }
        return assignStoreList;
    }

    @Override
    public List<StoreEntity> getUserAssignStoreList(Long userId) {

        List<StoreEntity> storeList = baseMapper.selectUserAssignStoreList(userId, CommonEnum.ZERO.getValue());
        if (storeList.isEmpty()) {
            log.info("查询用户被分单商店结果为空");
        }
        return storeList;
    }

    @Override
    public Integer getUserAssignStoreCount(Long userId) {
        return baseMapper.selectUserAssignStoreCount(userId, CommonEnum.ZERO.getValue());
    }

    @Override
    public Set<Long> getStoreUserIdSet(Long userId) {
        return baseMapper.selectStoreUserIdSet(userId, CommonEnum.ZERO.getValue());
    }

    @Override
    public void assignStore(AssignVo assignVo) {
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        List<AssignParamVo> assignParamVoList = assignVo.getStoreVoList();
        List<Long> storeIdList = assignParamVoList.stream().map(AssignParamVo::getStoreId).collect(Collectors.toList());

        LambdaUpdateWrapper<AssignStoreEntity> assignStoreLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 设置字段值
        assignStoreLambdaUpdateWrapper.set(AssignStoreEntity::getUserId, assignVo.getUserId())
                .set(AssignStoreEntity::getAssignType, assignVo.getAssignType())
                .set(AssignStoreEntity::getUpdateTime, date)
                .set(AssignStoreEntity::getUpdateUser, userPo.getUsername())
                .set(AssignStoreEntity::getUpdateUserId, userPo.getUserId());
        // 匹配条件
        assignStoreLambdaUpdateWrapper.in(AssignStoreEntity::getStoreId, storeIdList)
                .eq(AssignStoreEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 更新
        int count = baseMapper.update(null, assignStoreLambdaUpdateWrapper);
        log.info("商店分单操作完成。count={}", count);
    }

    @Override
    public void batchDeleteAssignStore(List<Long> storeIdList, List<Integer> submitTypeList) {
        log.debug("删除商店审核分单记录参数：storeIdList={} submitTypeList={}", storeIdList, submitTypeList);
        if (CollectionUtils.isEmpty(storeIdList)) {
            log.info("传入的商店主键列表为空");
            return;
        }
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<AssignStoreEntity> assignStoreLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        assignStoreLambdaUpdateWrapper.set(AssignStoreEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                .set(AssignStoreEntity::getUpdateTime, date)
                .set(AssignStoreEntity::getUpdateUser, userPo.getUsername())
                .set(AssignStoreEntity::getUpdateUserId, userPo.getUserId());
        assignStoreLambdaUpdateWrapper.in(AssignStoreEntity::getStoreId, storeIdList)
                .in(!CollectionUtils.isEmpty(submitTypeList), AssignStoreEntity::getSubmitType, submitTypeList)
                .eq(AssignStoreEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 批量删除
        int count = baseMapper.update(null, assignStoreLambdaUpdateWrapper);
        log.info("商店审核分单记录删除完成。count={}", count);
    }
}